import Vue from 'vue'
import VueRouter from 'vue-router'
/* 一级 */
import NaviGation from '@/views/NaviGation'
/* 获取token */
import { getToken } from '@/utils/token'
import { Toast } from 'vant'
/* 登录注册懒加载 */
const LogIn = () => import('@/views/LogIn')
const RegisTer = () => import('@/views/RegisTer')
/* 二级路由 */
const HomePage = () => import('@/views/navigation/HomePage')
const LookingRoom = () => import('@/views/navigation/LookingRoom')
const InforMation = () => import('@/views/navigation/InforMation')
const MyCenter = () => import('@/views/navigation/MyCenter')
/* 我的页面路由 */
const MyFavorite = () => import('@/views/MyFavorite')
const MyRental = () => import('@/views/MyRental')
const PersonalData = () => import('@/views/PersonalData')
const ContactUs = () => import('@/views/ContactUs')
const CheckingRecord = () => import('@/views/CheckingRecord')
const BeHomeowner = () => import('@/views/BeHomeowner')
/* 首页 */
const CityList = () => import('@/views/CityList')
const GoZuHome = () => import('@/views/GoZuHome')
/* 搜索 */
const SearchCity = () => import('@/views/SearchCity')
/* 地图 */
const HoursMap = () => import('@/views/HoursMap')
Vue.use(VueRouter)

const router = new VueRouter({
  routes: [
    {
      path: '/',
      component: NaviGation,
      redirect: '/homepage',
      children: [
        {
          path: 'homepage',
          component: HomePage// 首页
        },
        {
          path: 'lookingroom',
          component: LookingRoom// 找房
        },
        {
          path: 'information',
          component: InforMation// 咨询
        },
        {
          path: 'mycenter',
          component: MyCenter // 我的
        }
      ]
    },
    {
      path: '/login',
      component: LogIn// 登录
    },
    {
      path: '/register',
      component: RegisTer// 注册
    },
    {
      path: '/myfavorite',
      component: MyFavorite// 我的收藏
    },
    {
      path: '/myrental',
      component: MyRental// 我的出租
    },
    {
      path: '/personaldata',
      component: PersonalData// 个人资料
    },
    {
      path: '/contactus',
      component: ContactUs// 联系我们
    },
    {
      path: '/checkingrecord',
      component: CheckingRecord// 看房记录
    },
    {
      path: '/behomeowner',
      component: BeHomeowner// 成为房主
    },
    {
      path: '/citylist',
      component: CityList// 城市列表
    }, {
      path: '/gozuhome',
      component: GoZuHome// 去出租
    },
    {
      path: '/searchcity',
      component: SearchCity// 搜索
    },
    {
      path: '/hourmap',
      component: HoursMap// 地图
    }
  ]
})

/* 路由白名单 */
const whiteRoute = ['/login', '/register', '/mycenter']

/* 路由前置守卫 */
router.beforeEach((to, from, next) => {
  const token = getToken()
  if (token) {
    next()
  } else if (whiteRoute.includes(to.path)) {
    next()
  } else {
    Toast.fail('请登录')
    next('/login')
  }
})
export default router
